const jwt = require('jsonwebtoken');
const fs = require("fs")
module.exports = {
    token:{
        sign(payload,privateKey){
            return jwt.sign(payload,privateKey)
        },
        verify(token){
            return new Promise((resolve,reject)=>{
                jwt.verify(token,privateKey,(err,data)=>{
                    if(err){
                        reject(err)
                    }else{
                        resolve(data);
                    }
                })
            })
        }
    },
    uploads(newimgfile,filepath){
        return new Promise((resolve,reject)=>{
            const readImg = fs.createReadStream(filepath);
            const newImg = fs.createWriteStream(newimgfile);
            readImg.pipe(newImg);
            readImg.on('end',(chunk)=>{
                console.log("写入完成");
                resolve(newimgfile);
            })
            readImg.on('error',(err)=>{
                console.log('写入失败');
                reject(err)
            })
        })
    }
}